package com.k.base.jdk.stream.listbean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class DuplicateTest {
    public static void main(String[] args) {
        User u = new User("1", "a1", 22);
        User u2 = new User("2", "a2", 23);
        User u3 = new User("3", "a3", 24);
        User u4 = new User("4", "a3", 25);
        User u5 = new User("4", "a1", 26);
        List<User> list = new ArrayList<User>();
        list.add(u);
        list.add(u2);
        list.add(u3);
        list.add(u4);
        list.add(u5);

        duplicateTest(list);
    }

    /**
     * 找出 List 中重复的元素
     *
     * @param list list
     */
    public static void duplicateTest(List<User> list) {
        // 找出 List 重复记录
        List<String> filterList = list.stream()
                .filter(a -> a.getName() != null)
                .collect(Collectors.groupingBy(User::getName, Collectors.counting()))
                .entrySet()
                .stream()
                .filter(c -> c.getValue() > 1)
                .map(Map.Entry::getKey)
                .collect(Collectors.toList());
        for (String uu : filterList) {
            System.out.println("Duplicate name:" + uu);
        }
    }
}
